package com.ruoyi.receipt.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//
import com.ruoyi.receipt.domain.*;
import com.ruoyi.receipt.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 例题题目管理Controller
 *
 * @author ruoyi
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/receipt/exampleSubjectManager")
public class TbExampleSubjectManagerController extends BaseController {
    @Autowired
    private ITbExampleSubjectManagerService tbExampleSubjectManagerService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ITbExampleSubjectRelationService tbExampleSubjectRelationService;
    @Autowired
    private ITbExampleSubjectOptionService tbExampleSubjectOptionService;
    @Autowired
    private ITbSubjectClusteService tbSubjectClusteService;
    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;
    @Autowired
    private ITbExampleSubjectAnalysisService tbExampleSubjectAnalysisService;

    /**
     * 查询例题题目管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExampleSubjectManager tbExampleSubjectManager) {
        startPage();
        List<TbExampleSubjectManager> list = tbExampleSubjectManagerService.selectTbExampleSubjectManagerList(tbExampleSubjectManager);
        return getDataTable(list);
    }
    /**
     * 导出例题题目管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:export')")
    @Log(title = "例题题目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbExampleSubjectManager tbExampleSubjectManager) {
        List<TbExampleSubjectManager> list = tbExampleSubjectManagerService.selectTbExampleSubjectManagerList(tbExampleSubjectManager);
        ExcelUtil<TbExampleSubjectManager> util = new ExcelUtil<TbExampleSubjectManager>(TbExampleSubjectManager.class);
        return util.exportExcel(list, "manager");
    }

    /**
     * 获取例题题目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(id);
        //查询题簇
        TbSubjectCluste tbSubjectCluste = tbSubjectClusteService.selectTbSubjectClusteById(tbExampleSubjectManager.getSubjectClusteId());
        tbExampleSubjectManager.setClusteName(tbSubjectCluste.getClusteName());
        //查询知识点
        TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectManager.getKnowledgeManagerId());
        tbExampleSubjectManager.setZsName(tbKnowledgeManager.getTitle());
        //获取全部选项
        String[] option;
        List<String> list = tbExampleSubjectOptionService.selectTbExampleSubjectOptionMangagerId(id);
        List<String> list1 = new ArrayList<>();
        for (int i = 0; i < list.size() ; i++) {
            list1.add(list.get(i));
        }
        option = list1.toArray(new String[list1.size()]);
        tbExampleSubjectManager.setOption(option);
        //获取正确的选项
        String[] correctoption;
        List<String> tbExampleSubjectRelations = tbExampleSubjectRelationService.selectTbExampleSubjectRelationMangagerId(id);
        List<String> list2 = new ArrayList<>();
        for (int j = 0; j < tbExampleSubjectRelations.size(); j++) {
            list2.add(tbExampleSubjectRelations.get(j));
        }
        correctoption = list2.toArray(new String[list2.size()]);
        tbExampleSubjectManager.setCorrectoption(correctoption);
        return AjaxResult.success(tbExampleSubjectManager);
    }

    /**
     * 新增例题题目管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:add')")
    @Log(title = "例题题目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExampleSubjectManager tbExampleSubjectManager) {
        TbExampleSubjectManager tbExampleSubjectManager1 = new TbExampleSubjectManager();
        long l = idWorker.nextId();//例题题目id
        Long baseRatio = tbExampleSubjectManager.getBaseRatio();
        Long diffictRatio = tbExampleSubjectManager.getDiffictRatio();
        Long importRatio = tbExampleSubjectManager.getImportRatio();
        tbExampleSubjectManager1.setKnowledgeScore(baseRatio + diffictRatio + importRatio);
        tbExampleSubjectManager1.setStatus(1);
        tbExampleSubjectManager1.setIsDel(0);
        tbExampleSubjectManager1.setId(l);
        tbExampleSubjectManager1.setBaseRatio(tbExampleSubjectManager.getBaseRatio());
        tbExampleSubjectManager1.setDiffictRatio(tbExampleSubjectManager.getDiffictRatio());
        tbExampleSubjectManager1.setImportRatio(tbExampleSubjectManager.getImportRatio());
        tbExampleSubjectManager1.setKnowledgeManagerId(tbExampleSubjectManager.getKnowledgeManagerId());
        tbExampleSubjectManager1.setSubjectClusteId(tbExampleSubjectManager.getSubjectClusteId());
        tbExampleSubjectManager1.setType(tbExampleSubjectManager.getType());
        tbExampleSubjectManager1.setTitle(tbExampleSubjectManager.getTitle());
        //把全部得选项存在选项表里
        String[] option = tbExampleSubjectManager.getOption();
        String[] correctoption = tbExampleSubjectManager.getCorrectoption();
        //将正确的数组放入一个集合
        TbExampleSubjectOption tbExampleSubjectOption = new TbExampleSubjectOption();
        tbExampleSubjectOption.setSubjectManagerId(l);
        tbExampleSubjectOption.setIsDel(0);
        for (int i = 0; i < option.length ; i++) {
            long l1 = idWorker.nextId();
            tbExampleSubjectOption.setId(l1);
            tbExampleSubjectOption.setContent(option[i]);
            if (Arrays.asList(correctoption).contains(option[i])){
                TbExampleSubjectRelation tbExampleSubjectRelation = new TbExampleSubjectRelation();
//                    //就证明是正确答案   l1就是选项id  l就是例题id  l2是中间表的id
                long l2 = idWorker.nextId();
                tbExampleSubjectRelation.setSubjectManagerId(l);
                tbExampleSubjectRelation.setIsDel(0);
                tbExampleSubjectRelation.setId(l2);
                tbExampleSubjectRelation.setSubjectOptionId(l1);
                tbExampleSubjectRelation.setSubjectOptionContent(option[i]);
                tbExampleSubjectRelationService.insertTbExampleSubjectRelation(tbExampleSubjectRelation);
            }

            tbExampleSubjectOptionService.insertTbExampleSubjectOption(tbExampleSubjectOption);
        }

        return toAjax(tbExampleSubjectManagerService.insertTbExampleSubjectManager(tbExampleSubjectManager1));
    }

    /**
     * 修改例题题目管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:edit')")
    @Log(title = "例题题目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExampleSubjectManager tbExampleSubjectManager) {
        //例题id
        Long id = tbExampleSubjectManager.getId();
        int i1 = tbExampleSubjectRelationService.deleteTbExampleSubjectRelationSMId(id);
        int i2 = tbExampleSubjectOptionService.deleteTbExampleSubjectOptionSMId(id);
        //新建一个新的实体进行更新
        TbExampleSubjectManager tbExampleSubjectManager1 = new TbExampleSubjectManager();
        Long baseRatio = tbExampleSubjectManager.getBaseRatio();
        Long diffictRatio = tbExampleSubjectManager.getDiffictRatio();
        Long importRatio = tbExampleSubjectManager.getImportRatio();
        tbExampleSubjectManager1.setKnowledgeScore(baseRatio + diffictRatio + importRatio);
        tbExampleSubjectManager1.setStatus(1);
        tbExampleSubjectManager1.setIsDel(0);
        tbExampleSubjectManager1.setBaseRatio(tbExampleSubjectManager.getBaseRatio());
        tbExampleSubjectManager1.setDiffictRatio(tbExampleSubjectManager.getDiffictRatio());
        tbExampleSubjectManager1.setImportRatio(tbExampleSubjectManager.getImportRatio());
        tbExampleSubjectManager1.setKnowledgeManagerId(tbExampleSubjectManager.getKnowledgeManagerId());
        tbExampleSubjectManager1.setSubjectClusteId(tbExampleSubjectManager.getSubjectClusteId());
        tbExampleSubjectManager1.setType(tbExampleSubjectManager.getType());
        tbExampleSubjectManager1.setTitle(tbExampleSubjectManager.getTitle());
        tbExampleSubjectManager1.setId(id);
        //根据例题id 删除答案表
        tbExampleSubjectOptionService.deleteTbExampleSubjectOptionSMId(id);
        //根据id删除答案中间表
        tbExampleSubjectRelationService.deleteTbExampleSubjectRelationSMId(id);
        //添加新的选项数据
        String[] option = tbExampleSubjectManager.getOption();
        String[] correctoption = tbExampleSubjectManager.getCorrectoption();
        TbExampleSubjectOption tbExampleSubjectOption = new TbExampleSubjectOption();
        tbExampleSubjectOption.setSubjectManagerId(id);
        tbExampleSubjectOption.setIsDel(0);
        for (int i = 0; i < option.length ; i++) {
            long l1 = idWorker.nextId();
            tbExampleSubjectOption.setId(l1);
            tbExampleSubjectOption.setContent(option[i]);
            if (Arrays.asList(correctoption).contains(option[i])){
                TbExampleSubjectRelation tbExampleSubjectRelation = new TbExampleSubjectRelation();
//                    //就证明是正确答案   l1就是选项id  l就是例题id  l2是中间表的id
                long l2 = idWorker.nextId();
                tbExampleSubjectRelation.setSubjectManagerId(id);
                tbExampleSubjectRelation.setIsDel(0);
                tbExampleSubjectRelation.setId(l2);
                tbExampleSubjectRelation.setSubjectOptionId(l1);
                tbExampleSubjectRelation.setSubjectOptionContent(option[i]);
                tbExampleSubjectRelationService.insertTbExampleSubjectRelation(tbExampleSubjectRelation);
            }
            tbExampleSubjectOptionService.insertTbExampleSubjectOption(tbExampleSubjectOption);
        }
        System.out.println(tbExampleSubjectManager1);
        return toAjax(tbExampleSubjectManagerService.updateTbExampleSubjectManager(tbExampleSubjectManager1));

    }
    /**
     * 删除例题题目管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectManager:remove')")
    @Log(title = "例题题目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
       // System.out.println(ids);
        //遍历去删除
        //删除选项
        TbExampleSubjectManager tbExampleSubjectManager=new TbExampleSubjectManager();
        for (int  i =0;i<ids.length;i++){
            tbExampleSubjectOptionService.deleteTbExampleSubjectOptionSMId(ids[i]);
            //遍历删除中间表
            tbExampleSubjectRelationService.deleteTbExampleSubjectRelationSMId(ids[i]);
            tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(ids[i]);
            //根据id查询例题 修改例题状态
            tbExampleSubjectManager.setIsDel(1);
            tbExampleSubjectAnalysisService.updateAnalysisDel(tbExampleSubjectManager.getId());
            tbExampleSubjectManagerService.updateTbExampleSubjectManager(tbExampleSubjectManager);
        }
        return AjaxResult.success();
    }

    /**
     * 获取树结构数据
     * @return
     */
    @GetMapping("/getTreeList")
    public List<TbSubjectCluste> getTreeList(){
        List<TbSubjectCluste> list = tbSubjectClusteService.selectClusteStatus(new TbSubjectCluste());
        return list;
    }
    @GetMapping("/listName/{clusteName}")
    public List<TbSubjectCluste> listName(@PathVariable String clusteName){
        List<TbSubjectCluste> tbSubjectClustes = tbExampleSubjectManagerService.selectClName(clusteName);
        return tbSubjectClustes;
    }
    @GetMapping("/selectManageStatus")
    public TableDataInfo selectManageStatus(TbExampleSubjectManager tbExampleSubjectManager){
        startPage();
        List<TbSubjectCluste> list = tbSubjectClusteService.selectClusteStatus(new TbSubjectCluste());
        return getDataTable(list);
    }
}
